package com.example.lastwork2.detail;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.TabHost;

import com.example.lastwork2.R;
import com.example.lastwork2.adapter.PageAdapter;
import com.example.lastwork2.fragments.MineFragment;
import com.example.lastwork2.fragments.OrderFragment;
import com.example.lastwork2.fragments.RecommendFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {

    //控件
    private TabLayout tab1;
    private ViewPager2 vp2;
    //声明子页面数据源，使用fragment集合存储
    private List<Fragment> fragments;
    //声明子页面适配器对象
    private PageAdapter pageAdapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //获取控件对象
        getViews();
        //初始化子页面数据资源
        initPages();
        //初始化子页面适配器对象
        pageAdapter = new PageAdapter(fragments,this);
        //设置选项卡滑动方向
        vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //给viewPage2绑定适配器
        vp2.setAdapter(pageAdapter);
        //关联vp2和tablayout
        TabLayoutMediator mediator = new TabLayoutMediator(
                tab1,
                vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        //选项卡文本
                        switch (position){
                            case 0:
                                tab.setText("首页");
                                break;
                            case 1:
                                tab.setText("订单");
                                break;
                            case 2:
                                tab.setText("我的");
                                break;
                        }
                    }
                }
        );
        //关联
        mediator.attach();








    }






    private void initPages() {
        fragments = new ArrayList<>();
        fragments.add(new RecommendFragment());
        fragments.add(new OrderFragment());
        fragments.add(new MineFragment());
    }

    private void getViews() {
        //获取控件对象
        tab1 = findViewById(R.id.tab1);
        vp2 = findViewById(R.id.vp2);

    }
}